# 服务端接收数据的缓冲区
from socket import *
host = ''
# 将缓冲区设为2
buffersize = 2
port = 9876
addr = (host, port)
tcpServerSocket = socket(AF_INET, SOCK_STREAM)
tcpServerSocket.bind(addr)
tcpServerSocket.listen()
print("server port :9876")
print('正在等待客户端连接')
tcpClientSocket, addr = tcpServerSocket.accept()
print('客户端已经连接', 'addr', '=', addr)

# 初始化一个bytes类型的变量，用于保存完整的客户端数据
fullDataBytes = b''
while True:
    # 每次最多读取2个字节的数据
    data = tcpClientSocket.recv(buffersize)
    # 将读取的字节数添加到fullDataBytes变量的后面
    fullDataBytes += data
    # 如果读取的字节数小于bufferSize,则终止循环
    if len(data) < buffersize:
        break;

# 按原始字节格式输出客户端发送过来的信息
print(fullDataBytes)
# 将完整的字节格式数据用ISO-8859-1格式解码，然后输出
print(fullDataBytes.decode('ISO-8859-1'))
tcpClientSocket.close()
tcpServerSocket.close()




